<template>
  <div class="table-box">
    <ProTable
      ref="proTable"
      title="会员等级列表"
      row-key="id"
      :indent="20"
      :columns="columns"
      :request-api="getMemberGradeList"
      :data-callback="dataCallback"
    >
      <template #tableHeader>
        <el-button v-auth="'add'" type="primary" :icon="CirclePlus" @click="openDialog('新增', initData)">
          新增会员等级
        </el-button>
      </template>
      <template #operation="scope">
        <el-button v-auth="'view'" type="primary" link :icon="EditPen" @click="openDialog('查看', scope.row)"> 查看 </el-button>
        <el-button v-auth="'edit'" type="primary" link :icon="EditPen" @click="openDialog('编辑', scope.row)"> 编辑 </el-button>
        <el-button v-auth="'delete'" type="primary" link :icon="Delete" @click="deleteCategory(scope.row)"> 删除 </el-button>
      </template>
    </ProTable>
    <MemberGradeDialog ref="dialogRef" />
  </div>
</template>

<script setup lang="tsx" name="memberInfo">
import { ref, reactive } from "vue";
import { Member } from "@/api/interface";
import ProTable from "@/components/ProTable/index.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { useHandleData } from "@/hooks/useHandleData";
import { memberGradeDefault, memberGradeUpgrade } from "@/utils/serviceDict";
import { getMemberGradeList, editMemberGrade, addMemberGrade, deleteMemberGrade, getPriority } from "@/api/modules/member";
import MemberGradeDialog from "@/views/member/components/MemberGradeDialog.vue";
import { CirclePlus, EditPen, Delete } from "@element-plus/icons-vue";

const dataCallback = (data: any) => {
  return {
    list: data.list,
    total: data.total,
    pageNum: data.pageNum,
    pageSize: data.pageSize
  };
};
const proTable = ref<ProTableInstance>();
// 新建默认数据
const initData = reactive<any>({
  isDefault: false,
  isOpenUpgrade: false,
  prioritySortId: 1
});

const columns: ColumnProps<Member.ResMemberGradeList>[] = [
  { type: "index", label: "序号", width: 80 },
  { sortable: true, prop: "title", label: "等级名称", align: "left", search: { el: "input" } },
  {
    sortable: true,
    prop: "discountTypeName",
    label: "折扣类型",
    width: 150,
    align: "left"
  },
  {
    sortable: true,
    prop: "isDefault",
    label: "是否默认",
    enum: memberGradeDefault,
    width: 120,
    align: "left",
    search: { el: "select", props: { filterable: true } },
    fieldNames: { label: "label", value: "value" }
  },
  {
    sortable: true,
    prop: "prioritySortId",
    label: "升级优先级",
    width: 140,
    align: "left"
  },
  {
    sortable: true,
    prop: "isOpenUpgrade",
    label: "自动升级",
    enum: memberGradeUpgrade,
    width: 120,
    align: "left",
    search: { el: "select", props: { filterable: true } },
    fieldNames: { label: "label", value: "value" }
  },
  { sortable: true, prop: "rechargeAmount", label: "充值总金额", align: "left", width: 130 },
  { sortable: true, prop: "expenditureMoney", label: "消费总金额", align: "left", width: 130 },
  { prop: "operation", label: "操作", fixed: "right" }
];

const deleteCategory = async (params: Member.ResMemberGradeList) => {
  await useHandleData(deleteMemberGrade, { id: params.id }, `删除【${params.title}】会员等级`);
  proTable.value?.getTableList();
};

const dialogRef = ref<InstanceType<typeof MemberGradeDialog> | null>(null);
const openDialog = async (title: string, row: Partial<Member.ResMemberGradeList> = {}) => {
  const res = await getPriority({});
  initData.prioritySortId = res.data;
  if (!row.discountValue) {
    row.discountValue = 100;
  }
  const params = {
    title,
    row: { ...row, discountType: title === "新增" ? 2 : row.discountType },
    isView: title === "查看",
    api: title === "新增" ? addMemberGrade : title === "编辑" ? editMemberGrade : undefined,
    getTableList: proTable.value?.getTableList
  };
  dialogRef.value?.acceptParams(params);
};
</script>
